package com.sunspring.jbug.controller;

import com.sunspring.jbug.bean.OperationLog;
import com.sunspring.jbug.common.LogEnum;
import com.sunspring.jbug.common.SimpleResponse;
import com.sunspring.jbug.param.BugParam;
import com.sunspring.jbug.service.BugService;
import com.sunspring.jbug.service.FileService;
import com.sunspring.jbug.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wulei
 * @date 2019-03-06
 **/
@RestController
@RequestMapping("/bug")
public class BugController {

    @Autowired
    private BugService bugService;

    @Autowired
    private FileService fileService;

    @Autowired
    private OperationLogService ols;


    /**
     * 查看Bug简表
     */
    @GetMapping("/queryMinList")
    public SimpleResponse queryMinList() {

    }

    /**
     * 上传bug
     */
    @PostMapping("/add")
    public SimpleResponse add(BugParam bugParam, MultipartFile attachment) throws IOException {
        if (attachment != null) {
            String attachmentPath = fileService.save(attachment);
            if (attachmentPath != null) {
                bugParam.setAttachment(attachmentPath);
            }
        }
        Integer budId = bugService.add(bugParam);
        //添加操作记录
        OperationLog operationLog = OperationLog.builder()
                .aboutId(budId)
                .type(LogEnum.BUG.getCode())
                .message("吴磊提交了bug")
                .build();
        ols.addLog(operationLog);
        return SimpleResponse.success();
    }
}
